package com.bilicraft.plugin.kexue.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.bukkit.command.CommandSender;

/** for '_unzip' */
public class Zip {
	public static void unzip(CommandSender sender, String zipFileName, String outputDirectory) {
        try {
            ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
            ZipEntry z = in.getNextEntry();
            sender.sendMessage("Now unzip:");
            while (z != null) {
            	sender.sendMessage("- [Z]unziping " + z.getName());
                File f = new File(outputDirectory);
                f.mkdir();
                if (z.isDirectory()) {
                    String name = z.getName();
                    name = name.substring(0, name.length() - 1);
                    sender.sendMessage("- [Z]name " + name);
                    f = new File(outputDirectory + File.separator + name);
                    f.mkdir();
                    sender.sendMessage("- [Z]mkdir " + outputDirectory + File.separator + name);
                }
                else {
                    f = new File(outputDirectory + File.separator + z.getName());
                    f.createNewFile();
                    FileOutputStream out = new FileOutputStream(f);
                    int b;
                    while ((b = in.read()) != -1) {
                        out.write(b);
                    }
                    out.close();
                }
                z = in.getNextEntry();
            }
            in.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
